package com.zb.service.impl.web;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.zb.entity.ZjAuthor;
import com.zb.entity.ZjCustomerAuthor;
import com.zb.entity.vo.CustomerAuthorDetailVo;
import com.zb.entity.vo.CustomerAuthorSaveVo;
import com.zb.entity.vo.CustomerAuthorVo;
import com.zb.service.ZjAuthorService;
import com.zb.service.ZjCustomerAuthorService;
import com.zb.service.web.ConCustomerAuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author WEN
 * @Date 2024/2/20 0020
 **/
@Service
public class ConCustomerAuthorServiceImpl implements ConCustomerAuthorService {
    @Autowired
    private ZjCustomerAuthorService customerAuthorService;
    @Autowired
    private ZjAuthorService authorService;

    // 保存或修改商家模块菜单权限
    @Override
    @Transactional
    public void saveCustomerAuthor(Long id, CustomerAuthorSaveVo authorSaveVo) {
        QueryWrapper<ZjAuthor> authorQueryWrapper = new QueryWrapper<>();
        authorQueryWrapper.select("id", "name", "sort", "is_button AS button", "is_disabled AS disabled", "title");
        List<ZjAuthor> authorList = authorService.list(authorQueryWrapper);

        QueryWrapper<ZjCustomerAuthor> customerAuthorQueryWrapper = new QueryWrapper<>();
        customerAuthorQueryWrapper.select("id", "author_id", "parent_author_id", "name", "sort")
                .eq("cust_acc_id", id);
        List<ZjCustomerAuthor> customerAuthorList = customerAuthorService.list(customerAuthorQueryWrapper);

        List<ZjCustomerAuthor> addAuthorList = new ArrayList<>();
        List<ZjCustomerAuthor> updateAuthorList = new ArrayList<>();
        ZjCustomerAuthor customerAuthor = null;
        ZjAuthor author = null;
        for (CustomerAuthorVo vo : authorSaveVo.getAuthorVos()) {
            customerAuthor = customerAuthorList.stream().filter(f -> f.getParentAuthorId() == 0 && Objects.equals(f.getAuthorId(), vo.getAuthorId())).findFirst().orElse(null);
            if (null == customerAuthor) {
                customerAuthor = new ZjCustomerAuthor();
                customerAuthor.setId(IdWorker.getId());
                customerAuthor.setCustAccId(id);
                customerAuthor.setAuthorId(vo.getAuthorId());
                customerAuthor.setParentAuthorId(vo.getParentAuthorId());
                customerAuthor.setDisabled(vo.getStatus());
                author = authorList.stream().filter(f -> Objects.equals(f.getId(), vo.getAuthorId())).findFirst().orElse(null);
                if (null != author) {
                    customerAuthor.setName(author.getTitle());
                    customerAuthor.setSort(author.getSort());
                    customerAuthor.setDisabled(author.getDisabled());
                    customerAuthor.setButton(author.getButton());
                }
                addAuthorList.add(customerAuthor);
            } else {
                customerAuthor.setAuthorId(vo.getAuthorId());
                customerAuthor.setParentAuthorId(vo.getParentAuthorId());
                customerAuthor.setDisabled(vo.getStatus());
                updateAuthorList.add(customerAuthor);
            }
            getCustomerAuthorData(authorList, customerAuthorList, vo.getAuthorVos(), id, addAuthorList, updateAuthorList);
        }
        customerAuthorService.deleteByCustomerIdAndAuthorId(id, authorSaveVo.getDelAuthorIds());
        if (CollectionUtil.isNotEmpty(addAuthorList)) customerAuthorService.saveBatch(addAuthorList);
        if (CollectionUtil.isNotEmpty(updateAuthorList)) customerAuthorService.updateBatchById(updateAuthorList);
    }

    private void getCustomerAuthorData(List<ZjAuthor> authorList, List<ZjCustomerAuthor> customerAuthorList, List<CustomerAuthorVo> authorVos,
                                       Long id, List<ZjCustomerAuthor> addAuthorList, List<ZjCustomerAuthor> updateAuthorList) {
        if (CollectionUtil.isEmpty(authorVos)) {
            return;
        }
        ZjCustomerAuthor customerAuthor = null;
        ZjAuthor author = null;
        for (CustomerAuthorVo vo : authorVos) {
            customerAuthor = customerAuthorList.stream().filter(f -> Objects.equals(f.getAuthorId(), vo.getAuthorId())).findFirst().orElse(null);
            if (null == customerAuthor) {
                customerAuthor = new ZjCustomerAuthor();
                customerAuthor.setId(IdWorker.getId());
                customerAuthor.setCustAccId(id);
                customerAuthor.setAuthorId(vo.getAuthorId());
                customerAuthor.setParentAuthorId(vo.getParentAuthorId());
                customerAuthor.setDisabled(vo.getStatus());
                author = authorList.stream().filter(f -> Objects.equals(f.getId(), vo.getAuthorId())).findFirst().orElse(null);
                if (null != author) {
                    customerAuthor.setName(author.getTitle());
                    customerAuthor.setSort(author.getSort());
                    customerAuthor.setDisabled(author.getDisabled());
                    customerAuthor.setButton(author.getButton());
                }
                addAuthorList.add(customerAuthor);
            } else {
                customerAuthor.setAuthorId(vo.getAuthorId());
                customerAuthor.setParentAuthorId(vo.getParentAuthorId());
                customerAuthor.setDisabled(vo.getStatus());
                updateAuthorList.add(customerAuthor);
            }
            getCustomerAuthorData(authorList, customerAuthorList, vo.getAuthorVos(), id, addAuthorList, updateAuthorList);
        }

    }

}
